package com.liu.zero;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.liu.model.Student;
import com.liu.utils.DBRsHelp;
import com.mysql.fabric.xmlrpc.base.Struct;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;

public class zreoJdbc {
    /**
     * JDBC链接，java操作数据库（Java Database Connectivity）
     * @param args
     */
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, SQLException {
        
        /* 连接池 */
        DruidDataSource druidDataSource = new DruidDataSource();
        /* 连接信息 */
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/demo?userUnicode=true&characterEncoding=utf8&userSSL=true");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("1234");
        druidDataSource.setMaxActive(10);
        /* 获取连接池中的连接 */
        DruidPooledConnection connection1 = druidDataSource.getConnection();
        /* 获取Statement 用于执行sql*/
        Statement statement1 = connection1.createStatement();
        /* 执行sql 返回结果集 可遍历打印查看 */
        ResultSet resultSet1 = statement1.executeQuery("select * from stu ");
        /* 查看是否关闭 */
        System.out.println(connection1.isDisable());
        /* 关闭连接 */
        statement1.close();
        resultSet1.close();
        connection1.close();

        DruidPooledConnection connection2 = druidDataSource.getConnection();
        Statement statement2 = connection2.createStatement();
        int resultSet2 = statement2.executeUpdate("insert into stu (name,age,sex,classs) values ('说的',11,1,12)");
        System.out.println(connection1.isDisable());
        statement1.close();
        resultSet1.close();
        connection1.close();
        statement2.close();
        connection2.close();
        System.out.println(connection1.isDisable());
        System.out.println(statement1.isClosed());
        System.out.println(resultSet1.isClosed());

    }
    class Stu{
        private Integer id;
        private String name;
        private Integer age;

        @Override
        public String toString() {
            return "Stu{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}
